<script type="text/javascript">
	var is_apply = false;
	var validator =false;
	AudioPlayer.setup("<?=JS_URL?>/audio-player/player.swf", {  
            width: 250,
            transparentpagebg: "yes"
        });  
		
	function save(){
		var options = {
			beforeSubmit:  showRequest,  // pre-submit callback
			success:       showResponse  // post-submit callback
		};
		
		<?php
		foreach($form_view_fields as $k => $field_config){
			if($field_config['type'] == 'textarea' && !empty($field_config['editor'])){
		?>
				$('#<?=$field_config['name']?>Editor').val(oEdit<?=$field_config['name']?>.getHTMLBody());
		<?php
			}
		}
		?>
		$('#frmManagement').ajaxSubmit(options);
	}

	function apply(){
		is_apply = true;
		save();
	}

	function showRequest(formData, jqForm, options) {
		var flag = false;
		// validate editor
		<?php foreach($form_view_fields as $k => $field_config){
			if($field_config['type'] == 'textarea' && !empty($field_config['editor'])){
		?>
				$('#<?=$field_config['name']?>Editor').css('visibility','hidden').css('position', 'absolute').show();
		<?php
			}
		}
		?>
	
		if(validator){
			if(validator.form())
				flag = true;
			else{
				validator.showErrors();
				// show_perm_denied(); // TODO Why???
			}
		$('#loader').fadeOut(300);
		return flag;
		}
	}

	function showResponse(responseText, statusText, xhr, $form) {
		if(responseText=='success'){
			if(is_apply == true){
				location.reload();
			} else {
				location.href=root+"admincp/"+module+"/#/save";
			}
		}
		
		if(responseText=='permission-denied'){
			show_perm_denied();
		}
		
		if(responseText=='File not support'){
			alert('File not support');
			$('#loader').fadeOut(300);
			show_perm_denied();
		}
		
	}

	// Config editor
	function config_editor(editor){
		editor.width = '732px';
		editor.height = '425px';
		editor.cmdAssetManager="modalDialogShow('"+root+"static/editor/assetmanager/assetmanager.php',640,445);";
		editor.css="<?=PATH_URL.'static/css/tv_app/detail.css?r=3'?>";
	}
	<?php
	// setup form  validate rules
	$rules = array();
	foreach($form_view_fields as $k=>$v){
		if(isset($v['validate_type'])){
			$field_rules = explode('|', $v['validate_type']);
			$options = $v['name'] .': {';
			foreach($field_rules as $key=>$val){
				$options .= $val;
				if($key < count($field_rules) -1)
					$options .= ',';
			} 
			$options .='}';
			$rules[] = $options;
		}
	}
	if(!empty($rules)) { ?>
	$(document).ready(function(){
		$('.video-fancy').fancybox({
			onStart: function(e){
				player = e.attr('rel');
			},
			onClosed: function(e){
				if(player != ''){
					jwplayer(player).stop();
					player = '';
				}
			}
		});	
	
	
		validator = $('#frmManagement').validate({
			rules : {
			<?php  foreach($rules as $k => $v) {
				echo $v;
				if($k < count($rules) -1)
					echo ",";
			} ?>
			}
		});
	})
	<?php } ?>
</script>
<div class="gr_perm_error" style="display:none;">
	<p><strong>FAILURE: </strong><span id="txt_error">Permission Denied.</span></p>
</div>
<div class="table">
	<div class="head_table"><div class="head_title_edit"><?=$module?></div></div>
	<div class="clearAll"></div>

	<form id="frmManagement" action="<?=PATH_URL.'admincp/'.$module.'/save/'?>" method="post" enctype="multipart/form-data">
		<input type="hidden" value="<?=$id?>" name="hiddenIdAdmincp" id="hiddenIdAdmincp" />
		
		<?php 
		foreach($form_view_fields as $k => $v){
			echo $v['html'];
		}
		?>
		
		
		<!--

		<div class="row_text_field">
			<table cellspacing="0" cellpadding="0" border="0" width="100%">
				<tr>
					<td class="left_text_field">Month From:</td>
					<td class="right_text_field"><input value="<?php if(isset($result->week)) { print $result->week; }else{ print '';} ?>" type="text" name="weekAdmincp" /></td>
				</tr>
			</table>
		</div>

		<div class="row_text_field">
			<table cellspacing="0" cellpadding="0" border="0" width="100%">
				<tr>
					<td class="left_text_field">Month To:</td>
					<td class="right_text_field"><input value="<?php if(isset($result->week_to)) { print $result->week_to; }else{ print '';} ?>" type="text" name="weekToAdmincp" /></td>
				</tr>
			</table>
		</div>

		<div class="row_text_field">
			<table cellspacing="0" cellpadding="0" border="0" width="100%">
				<tr>
					<td class="left_text_field">Title:</td>
					<td class="right_text_field"><input value="<?php if(isset($result->title)) { print $result->title; }else{ print '';} ?>" type="text" name="titleAdmincp" /></td>
				</tr>
			</table>
		</div>
		<div class="row_text_field">
			<table cellspacing="0" cellpadding="0" border="0" width="100%">
				<tr>
					<td class="left_text_field">Chế độ ăn:</td>
					<td class="right_text_field" style="padding-right: 0px;">
						<textarea name="cdAnAdmincp" id="cdAnAdmincp" cols="" rows="8">
							<?php if(isset($result->cd_an)) { print $result->cd_an; }else{ print '';} ?>
						</textarea>
						<script type="text/javascript">
							var oEditCdAn = new InnovaEditor("oEditCdAn");
							config_editor(oEditCdAn);
							oEditCdAn.REPLACE("cdAnAdmincp");
						</script>
					</td>
				</tr>
			</table>
		</div>
		<div class="row_text_field">
			<table cellspacing="0" cellpadding="0" border="0" width="100%">
				<tr>
					<td class="left_text_field">Thực đơn cho bé:</td>
					<td class="right_text_field" style="padding-right: 0px;">
						<textarea name="thucDonAdmincp" id="thucDonAdmincp" cols="" rows="8">
							<?php if(isset($result->thuc_don)) { print $result->thuc_don; }else{ print '';} ?>
						</textarea>
						<script type="text/javascript">
							var oEditThucDon = new InnovaEditor("oEditThucDon");
							config_editor(oEditThucDon);
							oEditThucDon.REPLACE("thucDonAdmincp");
						</script>
					</td>
				</tr>
			</table>
		</div>
		<div class="row_text_field">
			<table cellspacing="0" cellpadding="0" border="0" width="100%">
				<tr>
					<td class="left_text_field">Chăm sóc bé:</td>
					<td class="right_text_field" style="padding-right: 0px;">
						<textarea name="chamSocAdmincp" id="chamSocAdmincp" cols="" rows="8">
							<?php if(isset($result->cham_soc)) { print $result->cham_soc; }else{ print '';} ?>
						</textarea>
						<script type="text/javascript">
							var oEditChamSoc = new InnovaEditor("oEditChamSoc");
							config_editor(oEditChamSoc);
							oEditChamSoc.REPLACE("chamSocAdmincp");
						</script>
					</td>
				</tr>
			</table>
		</div>
		<div class="row_text_field">
			<table cellspacing="0" cellpadding="0" border="0" width="100%">
				<tr>
					<td class="left_text_field">Vấn đề thường gặp</td>
					<td class="right_text_field" style="padding-right: 0px;">
						<textarea name="vanDeAdmincp" id="vanDeAdmincp" cols="" rows="8">
							<?php if(isset($result->van_de)) { print $result->van_de; }else{ print '';} ?>
						</textarea>
						<script type="text/javascript">
							var oEditVanDe = new InnovaEditor("oEditVanDe");
							config_editor(oEditVanDe);
							oEditVanDe.REPLACE("vanDeAdmincp");
						</script>
					</td>
				</tr>
			</table>
		</div>
		<div class="row_text_field">
			<table cellspacing="0" cellpadding="0" border="0" width="100%">
				<tr>
					<td class="left_text_field">Sự phát triển của bé</td>
					<td class="right_text_field" style="padding-right: 0px;">
						<textarea name="phatTrienAdmincp" id="phatTrienAdmincp" cols="" rows="8">
							<?php if(isset($result->phat_trien)) { print $result->phat_trien; }else{ print '';} ?>
						</textarea>
						<script type="text/javascript">
							var oEditPhatTrien = new InnovaEditor("oEditPhatTrien");
							config_editor(oEditPhatTrien);
							oEditPhatTrien.REPLACE("phatTrienAdmincp");
						</script>
					</td>
				</tr>
			</table>
		</div>
		<div class="row_text_field">
			<table cellspacing="0" cellpadding="0" border="0" width="100%">
				<tr>
					<td class="left_text_field">Các bước đệm cho<br/>sựphát triển của bé</td>
					<td class="right_text_field" style="padding-right: 0px;">
						<textarea name="buocDemAdmincp" id="buocDemAdmincp" cols="" rows="8">
							<?php if(isset($result->buoc_dem)) { print $result->buoc_dem; }else{ print '';} ?>
						</textarea>
						<script type="text/javascript">
							var oEditBuocDem = new InnovaEditor("oEditBuocDem");
							config_editor(oEditBuocDem);
							oEditBuocDem.REPLACE("buocDemAdmincp");
						</script>
					</td>
				</tr>
			</table>
		</div-->
	</form>
</div>